package www.common.util;


import www.common.entity.DTreeEntity;
import www.common.entity.DTreeIsCheckedEntity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 权限数据处理
 *
 * @author ruoyi
 */
public class DTreeUtils {
	/**
	 * 设置选中状态
	 */
	public static List<DTreeIsCheckedEntity> hasCheckArr(){
		//  设置选中状态
		DTreeIsCheckedEntity dTreeIsCheckedEntity = new DTreeIsCheckedEntity();
		dTreeIsCheckedEntity.setType("0");
		dTreeIsCheckedEntity.setIsChecked("1");
		List<DTreeIsCheckedEntity> checkArr = new ArrayList<DTreeIsCheckedEntity>();
		checkArr.add(dTreeIsCheckedEntity);
		return checkArr;
	}
	/**
	 * 根据父节点的ID获取所有子节点
	 *
	 * @param list   分类表
	 * @return String
	 */
	public static List<DTreeEntity> getChildPerms(List<DTreeEntity> list, int parentId) {
		List<DTreeEntity> returnList = new ArrayList<DTreeEntity>();
		for (Iterator<DTreeEntity> iterator = list.iterator(); iterator.hasNext(); ) {
			DTreeEntity t = (DTreeEntity) iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (t.getParentId() == parentId) {
				// 第一层
				t.setLevel(0);
				recursionFn(list, t);
				returnList.add(t);
			}
		}
		return returnList;
	}

	/**
	 * 递归列表
	 *
	 * @param list
	 * @param DTreeEntity
	 */
	private static void recursionFn(List<DTreeEntity> list, DTreeEntity t) {
		if (hasChild(list, t)) {
			t.setIsLast(false);
			// 得到子节点列表
			List<DTreeEntity> childList = getChildList(list, t);
			t.setChildren(childList);
			for (DTreeEntity tChild : childList) {
				if (hasChild(list, tChild)) {
					tChild.setIsLast(false);
					// 判断是否有子节点
					Iterator<DTreeEntity> it = childList.iterator();
					while (it.hasNext()) {
						DTreeEntity n = (DTreeEntity) it.next();
						recursionFn(list, n);
					}
				}else{
					tChild.setIsLast(true);
				}
			}
		}else{
			t.setIsLast(true);
		}
	}

	/**
	 * 得到子节点列表
	 */
	private static List<DTreeEntity> getChildList(List<DTreeEntity> list, DTreeEntity t) {

		List<DTreeEntity> tlist = new ArrayList<DTreeEntity>();
		Iterator<DTreeEntity> it = list.iterator();
		while (it.hasNext()) {
			DTreeEntity n = (DTreeEntity) it.next();
			if (n.getParentId() == t.getId()) {
				n.setLevel(t.getLevel() + 1);
				tlist.add(n);
			}
		}
		return tlist;
	}

	List<DTreeEntity> returnList = new ArrayList<DTreeEntity>();

	/**
	 * 根据父节点的ID获取所有子节点
	 *
	 * @param list   分类表
	 * @param typeId 传入的父节点ID
	 * @param prefix 子节点前缀
	 */
	public List<DTreeEntity> getChildPerms(List<DTreeEntity> list, int typeId, String prefix) {
		if (list == null) {
			return null;
		}
		for (Iterator<DTreeEntity> iterator = list.iterator(); iterator.hasNext(); ) {
			DTreeEntity node = (DTreeEntity) iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (node.getParentId() == typeId) {
				recursionFn(list, node, prefix);
			}
			// 二、遍历所有的父节点下的所有子节点
			/*
			 * if (node.getParentId()==0) { recursionFn(list, node); }
			 */
		}
		return returnList;
	}

	private void recursionFn(List<DTreeEntity> list, DTreeEntity node, String p) {
		// 得到子节点列表
		List<DTreeEntity> childList = getChildList(list, node);
		if (hasChild(list, node)) {
			// 判断是否有子节点
			returnList.add(node);
			Iterator<DTreeEntity> it = childList.iterator();
			while (it.hasNext()) {
				DTreeEntity n = (DTreeEntity) it.next();
				//n.setMenuName(p + n.getMenuName());
				recursionFn(list, n, p + p);
			}
		} else {
			returnList.add(node);
		}
	}

	/**
	 * 判断是否有子节点
	 */
	private static boolean hasChild(List<DTreeEntity> list, DTreeEntity t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}




}
